package com.hnkywl.sts.service.base;

import com.hnkywl.sts.dao.base.SjzdsyDao;
import com.hnkywl.sts.dao.base.SjzdzDao;
import com.hnkywl.sts.entity.base.Sjzdz;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-29
 * Time: 下午9:57
 * To change this template use File | Settings | File Templates.
 */
@Service
public class SjzdzService {
    private SjzdzDao sjzdzDao;
    private SjzdsyDao syzdsyDao;

    @Transactional
    public Sjzdz findSjzdzDetail(Long id) {
        return this.sjzdzDao.get(id);
    }

    @Transactional
    public String findAllSjzdz(String bm, int pageNo, int pageSize) {
        Page<Sjzdz> sjzdzPage = this.sjzdzDao.findSjzdzList(bm, pageNo, pageSize);
        List<Sjzdz> sjzdzList = sjzdzPage.getResult() == null ? new ArrayList<Sjzdz>() : sjzdzPage.getResult();
        String sjzdzJson = BaseUtils.fetchCollectionToJson(sjzdzList, "id", "bm", "z", "bz");
        return "{\"total\":" + sjzdzPage.getTotalCount() + ",\"rows\":[" + sjzdzJson + "]}";
    }

    @Transactional
    public void deleteSjzdz(Long id) {
        Sjzdz sjzdz = sjzdzDao.get(id);
        sjzdz.setLastUpdated(new Date());
        sjzdz.setDeleted(1);
        this.sjzdzDao.save(sjzdz);
    }


    /**
     * 根据名称查询数据字典
     *
     * @param type
     * @return
     */
    public Sjzdz findSjzdzByMc(String type) {
        String hql = "from Sjzdz t where t.z like ? ";
        List<Sjzdz> sjzdzList = this.sjzdzDao.find(hql, '%'+type+'%');
        if (null != sjzdzList && sjzdzList.size() > 0) {
            return sjzdzList.get(0);
        }
        return null;
    }

    @Autowired
    public void setSjzdzDao(SjzdzDao SjzdzDao) {
        this.sjzdzDao = SjzdzDao;
    }

}
